//******************************************************************************
//this code is protected by the GNU affero GPLv3
//author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
//                        <digital.ragnarok AT gmail dot com>
//******************************************************************************
//------------------------------------------------------------------------------
//compiler stuff
//------------------------------------------------------------------------------
#include <stdarg.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//ulinux stuff
//------------------------------------------------------------------------------
#include <ulinux/compiler_types.h>
#include <ulinux/types.h>
#include <ulinux/sysc.h>
#include <ulinux/error.h>

#include <ulinux/socket/socket.h>
#include <ulinux/socket/un.h>
#include <ulinux/socket/msg.h>

#include <ulinux/utils/ascii/ascii.h>
#include <ulinux/utils/ascii/string/string.h>
#include <ulinux/utils/ascii/string/vsprintf.h>

#include <ulinux/utils/mem.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//protocol
//------------------------------------------------------------------------------
#include <wayland/display.h>
#include <wayland/callback.h>
#include <wayland/compositor.h>
#include <wayland/registry.h>
#include <wayland/shm.h>
#include <wayland/shm_pool.h>
#include <wayland/buffer.h>
#include <wayland/seat.h>
#include <wayland/output.h>
#include <wayland/keyboard.h>
#include <wayland/pointer.h>
#include <wayland/wayland.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//our private stuff
//------------------------------------------------------------------------------
#include "globals.h"
#include "objs.h"
//------------------------------------------------------------------------------

#define REQ_DESTROY_DWS 2
void wl_shm_pool_buffer_req_destroy(k_u32 shm_pool_buffer)
{
  k_u32 req[REQ_DESTROY_DWS];
  req[0]=shm_pool_buffer;
  req[1]=(REQ_DESTROY_DWS*sizeof(k_u32))<<16|WL_BUFFER_DESTROY;

  struct k_io_vec iov;
  iov.base=&req;
  iov.len=REQ_DESTROY_DWS*sizeof(k_u32);

  struct k_msg_hdr m;
  u_memset(&m,0,sizeof(m));

  m.iov=&iov;
  m.iov_len=1;

  k_l r;
  do r=sysc(sendmsg,3,srv_so,&m,0);while(r==-K_EINTR);

  if(K_ISERR(r)){
    PERR("FATAL(%ld):unable to send the shm_pool_buffer destroy request\n",r);
    sysc(exit_group,1,-1);
  }
  if(r!=REQ_DESTROY_DWS*sizeof(k_u32)){
    PERR("FATAL:shm_pool_buffer::destroy request only %ld/%ld was sent\n",r,
                                                 REQ_DESTROY_DWS*sizeof(k_u32));
    sysc(exit_group,1,-1);
  }
  if(!r) sysc(exit_group,1,0);//server is gone
}
